﻿using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Util;

namespace DataCenter.Provider
{
    public class BilltoInfProvider : GenericProvider, IBilltoInfProvider
    {
        public List<BilltoInf> Search(out int totalRecords, bool? isLowPriceGroup = null, string textsearch = null, bool? transferFlag = null)
        {
            var querry = repository.GetQuery<BilltoInf>();
            if (isLowPriceGroup == true)
            {
                querry = querry.Where(x => !string.IsNullOrEmpty(x.LowPriceGroup));
            }

            if (!textsearch.IsNullOrWhiteSpace())
            {
                querry = querry.Where(x => x.BilltoCode.ToUpper().Contains(textsearch.ToUpper()) ||
                                         x.FullName.ToUpper().Contains(textsearch.ToUpper()) ||
                                         x.CustomerCode.ToUpper().Contains(textsearch.ToUpper()) ||
                                         x.Address.ToUpper().Contains(textsearch.ToUpper()));
            }

            if (transferFlag.HasValue)
            {
                string flag = transferFlag.Value.ToBooleanString();
                if (transferFlag.Value) querry = querry.Where(x => x.SoTransferFlag.ToUpper() == flag);
                else querry = querry.Where(x => x.SoTransferFlag == null || x.SoTransferFlag.Length == 0 || x.SoTransferFlag.ToUpper() == flag);
            }

            totalRecords = querry.Count();
            return querry.ToList();
        }

        public BilltoInf Retrieve(string billtocode)
        {
            if (billtocode.IsNullOrWhiteSpace()) return null;
            return repository.FindOne<BilltoInf>(x => x.BilltoCode.ToUpper() == billtocode.ToUpper());
        }

        public bool Add(BilltoInf billingInf)
        {
            try
            {
                repository.BilltoInf.Add(billingInf);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Update(BilltoInf billingInf)
        {
            var updateRecord = Retrieve(billingInf.BilltoCode);
            try
            {
                updateRecord.Address = billingInf.Address;
                updateRecord.Avtive = billingInf.Avtive;
                updateRecord.BilltoCode = billingInf.BilltoCode;
                updateRecord.BizNo = billingInf.BizNo;
                updateRecord.BuyingGroup = billingInf.BuyingGroup;
                updateRecord.CreditLevel = billingInf.CreditLevel;
                updateRecord.CustomerCode = billingInf.CustomerCode;
                updateRecord.FullName = billingInf.FullName;
                updateRecord.Grade = billingInf.Grade;
                updateRecord.HighPriceGroup = billingInf.HighPriceGroup;
                updateRecord.HighSaleChannel = billingInf.HighSaleChannel;
                updateRecord.LowPriceGroup = billingInf.LowPriceGroup;
                updateRecord.LowSaleChannel = billingInf.LowSaleChannel;
                updateRecord.PayFrom = billingInf.PayFrom;
                updateRecord.Profile = billingInf.Profile;
                updateRecord.Representative = billingInf.Representative;
                updateRecord.ShortName = billingInf.ShortName;
                updateRecord.TaxId = billingInf.TaxId;
                updateRecord.VnDepartment = billingInf.VnDepartment;
                updateRecord.SoTransferFlag = billingInf.SoTransferFlag;
                if (updateRecord.SoTransferFlag.IsYes()) updateRecord.SoTransferDate = DateTime.Now;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
